import axios from 'axios'
import { Message } from 'element-ui'

const instance = axios.create({
  baseURL: "http://localhost:3000/"
})

instance.interceptors.request.use(req => {
    if(localStorage.getItem("token")) {
      req.headers.Authorization = localStorage.getItem("token");
      console.log(localStorage.getItem("token"));
    }
    return req;
  } , err => {
    console.log(err);
    Promise.reject(err);
  })
  
  instance.interceptors.response.use(res => {
    console.log(res);
    console.log(res.status);
    if(res.data.code == 500) {
      if(res.data.msg) {
        console.log(res.data.msg);
        Message.error(res.data.msg);
      } else {
        Message.error('输入的数据有误，请重新输入！')
      }
    } else if(res.data.code == 200) {
      Message({
        message: res.data.msg,
        type: "success"
      })
    }
    return res;
  }, err => {
    console.log(err);
    Promise.reject(err);
  })
  
export default instance;
